import exceed from 'utils/apimap';
import { encryptPassword } from '../utils';

export function getUserHttpsAccount() {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'user.https.account',
      })
      .then((result) => {
        resolve(result);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

export function modifyUserHttpsUsername(newUsername) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'modifyUserHttpsUsername',
        data: {
          new_https_clone_username: newUsername,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

export function setHttpsCloneSecret(username, secret) {
  const encryptSecret = encryptPassword(secret);
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'setHttpsCloneSecret',
        data: {
          https_clone_username: username,
          https_clone_secret: encryptSecret,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

export function resetHttpsCloneSecret(username, secret, token) {
  const encryptSecret = encryptPassword(secret);
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'resetHttpsCloneSecret',
        data: {
          https_clone_username: username,
          https_clone_secret: encryptSecret,
          reset_https_clone_secret_token: token,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

export function sendResetHttpsCloneSecretToken() {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'sendResetHttpsCloneSecretToken',
      })
      .then((result) => {
        resolve(result);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

export function verifyUsername(username) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'verifyUsername',
        data: {
          https_clone_username: username,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

export function getScopes(data) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: data.vpc ? 'vpc.token.scope' : 'token.scope.get',
      })
      .then((result) => {
        resolve(result);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

export function generateToken(data, vpc = false) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: vpc ? 'vpc.token.single_post' : 'token.single.post',
        data,
      })
      .then((result) => {
        resolve(result);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

export function getTokenList(data) {
  return new Promise((resolve, reject) => {
    exceed[data.vpc ? 'fetch' : 'forceFetch']({
      api: data.vpc ? 'vpc.token.list' : 'token.list.get',
      data,
    })
      .then((result) => {
        resolve(result);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

export function deleteToken(id, vpc = false) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: vpc ? 'vpc.token.single_delete' : 'token.single.delete',
        params: {
          id,
        },
        data: {
          page: 1,
          per_page: 100,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

export function updateToken(id, data, vpc = false) {
  if (vpc) {
    return new Promise((resolve, reject) => {
      fetch(`${window.location.origin}/codeup/api/v4/oauth/personal_access_token/${id}`, {
        method: 'PUT',
        body: JSON.stringify(data),
        headers: new Headers({
          'Content-Type': 'application/json',
        }),
      })
        .then((res) => res.json())
        .then((result) => {
          resolve(result);
        })
        .catch((err) => reject(err));
    });
  } else {
    return new Promise((resolve, reject) => {
      exceed
        .fetch({
          api: 'token.single.put',
          params: {
            id,
          },
          data,
        })
        .then((result) => {
          resolve(result);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }
}

export default exceed;
